﻿using Microsoft.Win32;
using VisualWip.Core;
using VisualWip.Util;

namespace VisualWip
{
    public class WpfDialogPathSelector : IPathSelector
    {
        public WpfDialogPathSelector()
        {
            DefaultExtension = ".xml";
            DefaultFileName = "settings";
            Filter = "Xml documents (.xml)|*.xml";
        }

        public string DefaultExtension { get; set; }
        public string Filter { get; set; }

        public string DefaultFileName { get; set; }

        #region IFileNameSelector Members

        public PathSelectorResult OpenSelector()
        {
            var dlg = new OpenFileDialog();
            dlg.FileName = DefaultFileName;
            dlg.DefaultExt = DefaultExtension;
            dlg.Filter = Filter;

            bool? result = dlg.ShowDialog();
            if (result.HasValue)
                return new PathSelectorResult(result.Value,new SettingsPath(){Path = dlg.FileName});

            return new PathSelectorResult();
        }

        #endregion

        public PathSelectorResult SaveSelector()
        {
            var dlg = new SaveFileDialog();
            dlg.FileName = DefaultFileName;
            dlg.DefaultExt = DefaultFileName;
            dlg.Filter = Filter;

            bool? result = dlg.ShowDialog();
            if (result.HasValue)
                return new PathSelectorResult(result.Value, new SettingsPath() { Path = dlg.FileName });

            return new PathSelectorResult();
        }
    }
}